<?php
namespace Ucmaim\Model;
use Think\Model;


class ContentCategoryModel extends Model{

    // 获取内容分类列表
    public function getCategoryList($pid = 0, $label = ''){
        $map['pid'] = $pid;
        if( $label ) {
	        $label = strtolower($label);
	        $map['label'] = array('LIKE', "%$label%");
        }
        return $this->where($map)->select();
    }

    // 根据分类ID获取分类信息
    public function categoryInfo($id){
        if( ! is_numeric($id) ) return [];
        $map['id'] = $id;
        return $this->where($map)->find();
    }

    // 添加栏目
    public function addCategory($pid, $title, $label){
        if(! is_numeric($pid) || '' === trim($title))  return false;
        $data['pid'] = $pid;
        $data['name'] = $title;
        $data['label'] = strtolower($label);
        if($this->create($data)){
            $this->add();
            return true;
        }
        return false;
    }

    // 修改栏目信息
    public function updateCategory($id, $title, $label){
        if(! is_numeric($id) || '' === trim($title))  return false;
        $data['name'] = $title;
        $data['label'] = strtolower($label);
        $map['id'] = $id;
        return $this->where($map)->save($data) ? true : false;
    }

    public function deleteCategory($id){
        if(! is_numeric($id))  return false;
        $map['id'] = $id;
        return $this->where($map)->delete() ? true : false;
    }
}